package cn.edu.zafu.bigdata.yh;
import java.util.List;
import java.util.Comparator;

public class GenericMaxFinder {

    // 泛型方法，用于找到列表中的最大元素
    public static <T> T findMax(List<T> list, Comparator<T> comparator) {
        if (list == null || list.isEmpty()) {
            throw new IllegalArgumentException("List must not be null or empty");
        }
        T max = list.get(0);
        for (T element : list) {
            if (comparator.compare(max, element) < 0) {
                max = element;
            }
        }
        return max;
    }

    public static void main(String[] args) {
        // 测试泛型方法
        List<Integer> integerList = List.of(3, 5, 1, 9, 7);
        List<String> stringList = List.of("apple", "banana", "cherry", "date");

        // 查找整数列表中的最大值
        Integer maxInteger = findMax(integerList, Integer::compareTo);
        System.out.println("Max integer: " + maxInteger);

        // 查找字符串列表中的最大值
        String maxString = findMax(stringList, String::compareTo);
        System.out.println("Max string: " + maxString);
    }
}